﻿using GN.Pay.Core.Services;
using GN.Pay.TradeChannel;
using GN.Pay.TradeChannel.Transfers;
using GN.Pay.Utils;
using GN.Pay.Utils.Security;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GN.Pay.Core
{
    /// <summary>
    /// 扩展
    /// </summary>
    public static class PayExtensions
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="result"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        private static string ResultSignContent(ITradeOrderQueryResult result, string key)
        {
            var signDic = new SignDictionary();
            signDic["channelId"] = result.ChannelId;
            signDic["orderNo"] = result.OrderNo;
            signDic["tradeNo"] = result.TradeNo;
            signDic["payTime"] = result.PayTime.ToString("yyyy-MM-dd HH:mm:ss");
            signDic["orderMoney"] = result.OrderMoney.ToString("N2");
            signDic["payFee"] = result.PayFee.ToString("N2");
            return signDic.UrlLinkString() + "&key=" + key;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="result"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        private static string ResultSignContent(ITradeRefundApplyResult result, string key)
        {
            var signDic = new SignDictionary();
            signDic["channelId"] = result.ChannelId;
            signDic["refundNo"] = result.RefundNo;
            signDic["tradeNo"] = result.TradeNo;
            signDic["refundTime"] = result.RefundTime != null ? result.RefundTime.Value.ToString("yyyy-MM-dd HH:mm:ss") : "";
            signDic["refundMoney"] = result.RefundMoney.ToString("N2");
            signDic["refundFee"] = result.RefundFee.ToString("N2");
            return signDic.UrlLinkString() + "&key=" + key;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="result"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        private static string ResultSignContent(ITransfersApplyResult result, string key)
        {
            var signDic = new SignDictionary();
            signDic["channelId"] = result.ChannelId;
            signDic["transfersNo"] = result.TransfersNo;
            signDic["tradeNo"] = result.TradeNo;
            signDic["transfersTime"] = result.TransfersTime != null ? result.TransfersTime.Value.ToString("yyyy-MM-dd HH:mm:ss") : "";
            signDic["transfersMoney"] = result.TransfersMoney.ToString("N2");
            signDic["transfersFee"] = result.TransfersFee.ToString("N2");
            return signDic.UrlLinkString() + "&key=" + key;
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="result"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string MessageSign(this ITradeOrderQueryResult result, string key)
        {
            ExceptionUtils.CheckNotNull(result, nameof(result));
            ExceptionUtils.CheckNotNullOrNotWhiteSpace(key, nameof(key));
            return Md5Utils.Sign(ResultSignContent(result, key), Encoding.UTF8);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="result"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string MessageSign(this ITransfersApplyResult result, string key)
        {
            ExceptionUtils.CheckNotNull(result, nameof(result));
            ExceptionUtils.CheckNotNullOrNotWhiteSpace(key, nameof(key));
            return Md5Utils.Sign(ResultSignContent(result, key), Encoding.UTF8);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static bool MessageSignVerify(this ITradeOrderQueryResult result, string key, string sign)
        {
            ExceptionUtils.CheckNotNull(result, nameof(result));
            ExceptionUtils.CheckNotNullOrNotWhiteSpace(key, nameof(key));
            ExceptionUtils.CheckNotNullOrNotWhiteSpace(sign, nameof(sign));
            return Md5Utils.SginVerify(ResultSignContent(result, key), sign, Encoding.UTF8);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="result"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string MessageSign(this ITradeRefundApplyResult result, string key)
        {
            ExceptionUtils.CheckNotNull(result, nameof(result));
            ExceptionUtils.CheckNotNullOrNotWhiteSpace(key, nameof(key));
            return Md5Utils.Sign(ResultSignContent(result, key), Encoding.UTF8);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static bool MessageSignVerify(this ITradeRefundApplyResult result, string key, string sign)
        {
            ExceptionUtils.CheckNotNull(result, nameof(result));
            ExceptionUtils.CheckNotNullOrNotWhiteSpace(key, nameof(key));
            ExceptionUtils.CheckNotNullOrNotWhiteSpace(sign, nameof(sign));
            return Md5Utils.SginVerify(ResultSignContent(result, key), sign, Encoding.UTF8);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static bool MessageSignVerify(this ITransfersApplyResult result, string key, string sign)
        {
            ExceptionUtils.CheckNotNull(result, nameof(result));
            ExceptionUtils.CheckNotNullOrNotWhiteSpace(key, nameof(key));
            ExceptionUtils.CheckNotNullOrNotWhiteSpace(sign, nameof(sign));
            return Md5Utils.SginVerify(ResultSignContent(result, key), sign, Encoding.UTF8);
        }

    }
}
